﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Collections.ObjectModel;

namespace XamlCleaner.Core.Pharser
{
    public class DuplicatedNameSpace
    {
        #region Members
        /// <summary>
        /// Uri of NameSpace
        /// </summary>
        private string value;

        /// <summary>
        /// Name of Main NameSpace
        /// </summary>
        private string mainNameSpace;

        /// <summary>
        /// List of duplicated NameSpaces Names.
        /// </summary>
        private List<string> duplicatedNameSpaces = new List<string>();
        #endregion

        #region Constructors

        /// <summary>
        /// Data struct for Duplicated NameSpaces
        /// </summary>
        /// <param name="MainNameSpace">XmlAttribute of Main NameSpace</param>
        /// <param name="DuplicatedNameSpaces">List of XamlAttributes for duplicated NameSpaces</param>
        public DuplicatedNameSpace(XmlAttribute MainNameSpace, List<XmlAttribute> duplicatedNameSpaces)
        {
            if (MainNameSpace == null)
                throw new ArgumentNullException("mainNameSpace", "mainNameSpace is null.");

            this.value = MainNameSpace.Value;
            this.mainNameSpace = MainNameSpace.LocalName;

            if (duplicatedNameSpaces != null)
            {
                foreach (XmlAttribute item in duplicatedNameSpaces)
                {
                    this.duplicatedNameSpaces.Add(item.LocalName);
                }
            }

        }

        /// <summary>
        /// Data struct for Duplicated NameSpaces
        /// </summary>
        /// <param name="MainNameSpace">XmlAttribute of Main NameSpace</param>
        public DuplicatedNameSpace(XmlAttribute MainNameSpace)
            : this(MainNameSpace, null) { }

        /// <summary>
        /// Data struct for Duplicated NameSpaces
        /// </summary>
        /// <param name="NameSpaceValue">Uri of NameSpaces</param>
        /// <param name="MainNameSpaceName">Main NameSpace name</param>
        /// <param name="DuplicatedNameSpacesNames">List of duplicated NameSpaces names</param>
        public DuplicatedNameSpace(string NameSpaceValue, string MainNameSpaceName, List<string> DuplicatedNameSpacesNames)
        {
            this.value = NameSpaceValue;
            this.mainNameSpace = MainNameSpaceName;

            if (duplicatedNameSpaces != null)
            {
                foreach (string item in DuplicatedNameSpacesNames)
                {
                    this.duplicatedNameSpaces.Add(item);
                }
            }
        }

        /// <summary>
        /// Data struct for Duplicated NameSpaces
        /// </summary>
        /// <param name="NameSpaceUri">Uri of NameSpaces</param>
        /// <param name="MainNameSpaceName">Main NameSpace name</param>
        public DuplicatedNameSpace(string NameSpaceUri, string MainNameSpaceName)
            : this(NameSpaceUri, MainNameSpaceName, null) { }
        #endregion

        #region Properties

        /// <summary>
        /// Uri of NameSpace
        /// </summary>
        public string Value
        {
            get
            {
                return this.value;
            }
        }

        /// <summary>
        /// Name of Main NameSpace
        /// </summary>
        public string MainNameSpace
        {
            get
            {
                return this.mainNameSpace;
            }
        }

        /// <summary>
        /// List of duplicated NameSpaces Names.
        /// </summary>
        public ReadOnlyCollection<string> DuplicatedNameSpaces
        {
            get
            {
                return new ReadOnlyCollection<string>(this.duplicatedNameSpaces);
            }
        }
        #endregion

        #region Methods

        /// <summary>
        /// Add a name space as duplicated of MainNameSpace
        /// </summary>
        /// <param name="NameSpace">Name Space</param>
        public void AddDuplicatedNameSpace(string NameSpace)
        {
            this.duplicatedNameSpaces.Add(NameSpace);
        }

        /// <summary>
        /// Add a name space as duplicated of MainNameSpace
        /// </summary>
        /// <param name="NameSpace">Name Space</param>
        public void AddDuplicatedNameSpace(List<string> NameSpaces)
        {
            foreach (string item in NameSpaces)
            {
                this.AddDuplicatedNameSpace(item);
            }

        }

        /// <summary>
        /// Add a name space as duplicated of MainNameSpace
        /// </summary>
        /// <param name="NameSpace">Name Space</param>
        /// <exception cref="System.ArgumentException">
        /// When NameSpaceUri property of <paramref name="NameSpace"/> param 
        /// is NOT equals of NameSpaceUri property.
        /// </exception>
        public void AddDuplicatedNameSpace(XmlAttribute NameSpace)
        {
            if (!NameSpace.Value.Equals(this.Value))
                throw new ArgumentException(Resources.Messages.InvalidNameSpaceURI, "NameSpace");
            AddDuplicatedNameSpace(NameSpace.LocalName);
        }

        /// <summary>
        /// Add a name space as duplicated of MainNameSpace
        /// </summary>
        /// <param name="NameSpace">Name Space</param>
        /// <exception cref="System.ArgumentException">
        /// When NameSpaceUri property of <paramref name="NameSpace"/> param 
        /// is NOT equals of NameSpaceUri property.
        /// </exception>
        public void AddDuplicatedNameSpace(List<XmlAttribute> NameSpaces)
        {
            if (NameSpaces == null)
                throw new ArgumentNullException("NameSpaces", "NameSpaces is null.");
            foreach (XmlAttribute item in NameSpaces)
            {
                AddDuplicatedNameSpace(item);
            }

        }
        #endregion

        #region Overrides

        public override bool Equals(object obj)
        {
            if (obj.GetType() != typeof(DuplicatedNameSpace))
                return false;
            return ((DuplicatedNameSpace)obj).Value.Equals(this.Value);
        }

        public override int GetHashCode()
        {
            return this.Value.GetHashCode();
        }

        public override string ToString()
        {
            return this.Value;
        }

        #endregion
    }
}
